package meihua.stopcarapp.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.OnClick;
import meihua.mylibrary.base.BaseActivity;
import meihua.mylibrary.commonutils.RxCountDown;
import meihua.stopcarapp.R;
import meihua.stopcarapp.app.UserConfig;
import meihua.stopcarapp.model.StopCarModel;
import meihua.stopcarapp.presenter.StopCarPresenter;
import meihua.stopcarapp.presenter.contract.StopCarContract;
import meihua.stopcarapp.utils.ToastUtil;
import rx.Subscriber;
import rx.functions.Action0;

/**
 * Name: LGZ
 * Time: 2017/5/15.
 * Dec:
 */

public class StopCarActivity extends BaseActivity<StopCarPresenter,StopCarModel> implements StopCarContract.View {

    @Bind(R.id.dialog_stopcar_time_tv2)
    TextView dialogStopcarTimeTv2;
    @Bind(R.id.dialog_stopcar_time_tv1)
    TextView dialogStopcarTimeTv1;
    @Bind(R.id.dialog_stopcar_time_tv3)
    TextView dialogStopcarTimeTv3;
    @Bind(R.id.dialog_stopcar_time_tv4)
    TextView dialogStopcarTimeTv4;
    @Bind(R.id.dialog_stopcar_btn_tv)
    TextView dialogStopcarBtnTv;
    @Bind(R.id.activity_stopcar_progressbar)
    ProgressBar progressBar;

    int time1;
    int time2;
    int time3;
    int time4;

    boolean is_timing = true;
    String order_num;

    /**
     * 入口
     *
     * @param activity
     */
    public static void startAction(Activity activity,String order_num) {
        Intent intent = new Intent(activity, StopCarActivity.class);
        intent.putExtra("order_num",order_num);
        activity.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_stopcar;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this,mModel);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        Intent intent = getIntent();
        order_num = intent.getStringExtra("order_num");
        ToastUtil.showShort(StopCarActivity.this,order_num);
        fiveTime();
    }

    void fiveTime(){
        //发送短信的倒计时
        RxCountDown.countdown(60*1-1)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        ToastUtil.showShort(StopCarActivity.this,"开始");
                    }
                })
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                        //结束
                        startTiming();
                    }
                    @Override
                    public void onError(Throwable e) {
                    }
                    @Override
                    public void onNext(Integer integer) {
                        time1 = Integer.parseInt(dialogStopcarTimeTv1.getText().toString());
                        time2 = Integer.parseInt(dialogStopcarTimeTv2.getText().toString());
                        time3 = Integer.parseInt(dialogStopcarTimeTv3.getText().toString());
                        time4 = Integer.parseInt(dialogStopcarTimeTv4.getText().toString());
                        if (time4 > 0) {
                            time4--;
                        } else if (time3 > 0) {
                            time3--;
                            time4 = 9;
                        } else if (time2 > 0) {
                            time2--;
                            time3 = 5;
                            time4 = 9;
                        } else if (time1 > 0){
                            time1--;
                            time2 = 9;
                            time3 = 5;
                            time4 = 9;
                        }
                        dialogStopcarTimeTv1.setText(time1+"");
                        dialogStopcarTimeTv2.setText(time2+"");
                        dialogStopcarTimeTv3.setText(time3+"");
                        dialogStopcarTimeTv4.setText(time4+"");
                    }
                });
    }

    void startTiming(){
        ToastUtil.showShort(this,"开始计费");
        new Thread(new MyThread()).start();         // start thread
    }

    final Handler handler = new Handler(){          // handle
        public void handleMessage(Message msg){
            switch (msg.what) {
                case 1:
                    time1 = Integer.parseInt(dialogStopcarTimeTv1.getText().toString());
                    time2 = Integer.parseInt(dialogStopcarTimeTv2.getText().toString());
                    time3 = Integer.parseInt(dialogStopcarTimeTv3.getText().toString());
                    time4 = Integer.parseInt(dialogStopcarTimeTv4.getText().toString());
                    if (time4<9){
                        time4++;
                    }else if (time3<5){
                        time3++;
                        time4 = 0;
                    }else if (time2<9){
                        time2++;
                        time4 = 0;
                        time3 = 0;
                    }else if (time1<5){
                        time4 = 0;
                        time3 = 0;
                        time2 = 0;
                    }
                    dialogStopcarTimeTv1.setText(time1+"");
                    dialogStopcarTimeTv2.setText(time2+"");
                    dialogStopcarTimeTv3.setText(time3+"");
                    dialogStopcarTimeTv4.setText(time4+"");
            }
            super.handleMessage(msg);
        }
    };

    @OnClick(R.id.dialog_stopcar_btn)
    public void onViewClicked() {
        mPresenter.getChargingRequest(order_num, UserConfig.USER_ENTITY.getPark_list_info().getParking_list_num());
        is_timing = false;
    }

    @Override
    public void showLoading(String title) {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void stopLoading() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showErrorTip(String msg) {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void returnCharging(String money) {
        Bundle bundle = new Bundle();
        bundle.putString("order_monry",money);
        OrderEcodeActivity.startAction(this,bundle);
        this.finish();
    }

    public class MyThread implements Runnable {      // thread
        @Override
        public void run() {
            while (is_timing) {
                try {
                    Thread.sleep(1000*60);     // sleep 1000ms
                    Message message = new Message();
                    message.what = 1;
                    handler.sendMessage(message);
                } catch (Exception e) {
                }
            }
        }
    }

}
